package com.liner.controller;

import com.alibaba.excel.EasyExcel;
import com.liner.entity.ProductInfo;
import com.liner.form.SellerProductInfoForm;
import com.liner.form.SellerProductInfoUpdateForm;
import com.liner.handler.CustomCellWriteHandler;
import com.liner.result.Result;
import com.liner.service.ProductCategoryService;
import com.liner.service.ProductInfoService;
import com.liner.vo.ProductExcelVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 卖家端 商品服务接口
 */

@RestController
@RequestMapping("/seller/product")
@Api(tags = "卖家端商品接口", description = "卖家端 商品服务接口")
public class SellerProductController {

    @Autowired
    private ProductCategoryService productCategoryService;

    @Autowired
    private ProductInfoService productInfoService;

    @ApiOperation("查询所有商品分类")
    @GetMapping("/findAllProductCategory")
    public Result findAllProductCategory() {
        Map<String, List> map = new HashMap<>();
        map.put("content", productCategoryService.sellerList());
        return Result.ok(map);
    }

    @ApiOperation("添加商品")
    @PostMapping("/add")
    public Result addProductCategory(@RequestBody SellerProductInfoForm form) {
        ProductInfo productInfo = new ProductInfo();
        BeanUtils.copyProperties(form, productInfo);
        productInfoService.save(productInfo);
        return Result.ok();
    }

    @ApiOperation("查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "每页条数")
    })
    @GetMapping("/list/{page}/{size}")
    public Result list(@PathVariable Integer page, @PathVariable Integer size) {
        return Result.ok(productInfoService.sellerProductInfoVo(page, size));
    }


    @ApiOperation("商品模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyWord", value = "关键字"),
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "每页条数")
    })
    @GetMapping("/like/{keyWord}/{page}/{size}")
    public Result like(@PathVariable("keyWord") String keyWord,@PathVariable("page") Integer page,@PathVariable("size") Integer size) {
        return Result.ok(productInfoService.sellerProductInfoVoLike(keyWord, page, size));
    }

    @ApiOperation("通过分类查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryType", value = "分类"),
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "每页条数")
    })
    @GetMapping("/findByCategory/{categoryType}/{page}/{size}")
    public Result findByCategory(@PathVariable("categoryType") Integer categoryType,@PathVariable("page")  Integer page,@PathVariable("size")  Integer size) {
        return Result.ok(productInfoService.sellerProductInfoVoByCategoryType(categoryType, page, size));
    }

    @ApiOperation("通过ID查询商品")
    @ApiImplicitParam(name = "id", value = "ID")
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable Integer id) {
        return Result.ok(productInfoService.sellerProductInfoVoById(id));
    }

    @ApiOperation("通过ID删除商品")
    @ApiImplicitParam(name = "id", value = "ID")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.ok(productInfoService.removeById(id));
    }

    @ApiOperation("修改商品状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID"),
            @ApiImplicitParam(name = "status", value = "状态")
    })
    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Integer id, @PathVariable Boolean status) {
        ProductInfo productInfo = new ProductInfo();
        productInfo.setProductId(id);
        if (status) {
            productInfo.setProductStatus(1);
        } else {
            productInfo.setProductStatus(0);
        }
        return Result.ok(productInfoService.updateById(productInfo));
    }

    @ApiOperation("修改商品")
    @PutMapping("/update")
    public Result update(@RequestBody SellerProductInfoUpdateForm form) {
        ProductInfo productInfo = new ProductInfo();
        BeanUtils.copyProperties(form,productInfo);
        if (form.getStatus()){
            productInfo.setProductStatus(1);
        }else{
            productInfo.setProductStatus(0);
        }
        productInfo.setCategoryType(form.getCategory().getCategoryType());
        return Result.ok( productInfoService.updateById(productInfo));
    }

    @ApiOperation("导出Excel")
    @PutMapping("/export")
    public void exportExcel(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("UTF-8");
            String fileName = URLEncoder.encode("商品信息", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //获取ProductExcelVO类型的List
            List<ProductExcelVo> productExcelVOList = productInfoService.excelList();
            EasyExcel.write(response.getOutputStream(), ProductExcelVo.class)
                    .registerWriteHandler(new CustomCellWriteHandler())
                    .sheet("商品信息")
                    .doWrite(productExcelVOList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("导入Excel")
    @PutMapping("/import")
    public Result importExcel(@RequestParam("file") MultipartFile file) {

        List<ProductInfo> productInfoList = null;
        try {
            productInfoList = this.productInfoService.excelToProductInfoList(file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(productInfoList==null){
            return Result.fail("导入Excel失败！");
        }
        boolean result = this.productInfoService.saveBatch(productInfoList);
        if(result)return Result.ok();
        return Result.fail("导入Excel失败！");

    }









}
